﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace TestUp.Models.Helpers
{
    public class PermissionHelper
    {
        public List<string> GetRoles(Permission permission)
        {
            var list = new List<string>();
            if (permission != null)
            {
                if (permission.CanManageGroups) list.Add("ManageGroups");
                if (permission.CanManageTests) list.Add("ManageTests");
                if (permission.CanPassTests) list.Add("PassTests");
                if (permission.CanSeeAdminPanel) list.Add("AdminPanel");
            }
            return list;
        }

        public string ConvertToSingleRole(List<string> roles)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (roles != null && roles.Count > 0)
            {
                string splitter = ConfigurationManager.AppSettings["RolesSplitter"];
                for (int i = 0; i < roles.Count; i++)
                {
                    stringBuilder.Append(roles[i]);
                    if (i < roles.Count - 1)//don`t append splitter after the last role
                    {
                        stringBuilder.Append(splitter);
                    }
                }
            }
            return stringBuilder.ToString();
        }

        public Permission GetPermission(int permissionId)
        {
            return DataWorkItem.Entity.Permissions.FirstOrDefault(x => x.PermissionId == permissionId);
        }

        public List<Permission> GetAllPermissions()
        {
            return DataWorkItem.Entity.Permissions.ToList();
        }

        public void DetetePermission(int permissionId)
        {
            var permission = GetPermission(permissionId);

            if(permission!=null)
            {
                DataWorkItem.Entity.Permissions.Remove(permission);
                DataWorkItem.Entity.SaveChanges();
            }
        }

        public void UpdatePermission(Permission newPermission)
        {
            var originalPermission = GetPermission(newPermission.PermissionId);

            if(originalPermission != null)
            {
                //Update fields
                originalPermission.PermissionName = newPermission.PermissionName;
                originalPermission.CanManageGroups = newPermission.CanManageGroups;
                originalPermission.CanManageTests = newPermission.CanManageTests;
                originalPermission.CanPassTests = newPermission.CanPassTests;
                originalPermission.CanSeeAdminPanel = newPermission.CanSeeAdminPanel;

                DataWorkItem.Entity.SaveChanges();
            }
        }

        public void CreatePermission(Permission sourcePermission)
        {
            var newPermission = new Permission
                {
                    PermissionName = sourcePermission.PermissionName,
                    CanManageGroups = sourcePermission.CanManageGroups,
                    CanManageTests = sourcePermission.CanManageTests,
                    CanPassTests = sourcePermission.CanPassTests,
                    CanSeeAdminPanel = sourcePermission.CanSeeAdminPanel
                };
            DataWorkItem.Entity.Permissions.Add(newPermission);
            DataWorkItem.Entity.SaveChanges();
        }
    }
}